home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_023 / ver30 / random.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  11KB  |  448 lines

  1. /*
  2.  * Name:    MicroEMACS
  3.  *        Assorted commands.
  4.  * Version:    29
  5.  * Last edit:    10-Feb-86
  6.  * By:        rex::conroy
  7.  *        decvax!decwrl!dec-rhea!dec-rex!conroy
  8.  *
  9.  * The file contains the command
  10.  * processors for a large assortment of unrelated
  11.  * commands. The only thing they have in common is
  12.  * that they are all command processors.
  13.  */
  14. #include    "def.h"
  15.  
  16. /*
  17.  * Display a bunch of useful information about
  18.  * the current location of dot. The character under the
  19.  * cursor (in octal), the current line, row, and column, and
  20.  * approximate position of the cursor in the file (as a percentage)
  21.  * is displayed. The column position assumes an infinite position
  22.  * display; it does not truncate just because the screen does.
  23.  * This is normally bound to "C-X =".
  24.  */
  25. showcpos(f, n, k)
  26. {
  27.     register LINE    *clp;
  28.     register int    cbo;
  29.     register int    nchar;
  30.     register int    cchar;
  31.     register int    nline;
  32.     register int    cline;
  33.     register int    cbyte;
  34.     register int    ratio;
  35.     register int    row;
  36.     register int    col;
  37.     register int    i;
  38.     register int    c;
  39.  
  40.     clp = lforw(curbp->b_linep);        /* Collect the data.    */
  41.     cbo = 0;
  42.     nchar = 0;
  43.     nline = 1;                /* Origin 1.        */
  44.     for (;;) {
  45.         if (clp == curwp->w_dotp) {
  46.             cline = nline;
  47.             if (cbo == curwp->w_doto) {
  48.                 cchar = nchar;
  49.                 if (cbo == llength(clp))
  50.                     cbyte = '\n';
  51.                 else
  52.                     cbyte = lgetc(clp, cbo);
  53.             }
  54.         }
  55.         if (cbo == llength(clp)) {
  56.             if (clp == curbp->b_linep)
  57.                 break;
  58.             clp = lforw(clp);
  59.             cbo = 0;
  60.             ++nline;        /* Count a line.    */
  61.         } else
  62.             ++cbo;
  63.         ++nchar;            /* Count a character.    */
  64.     }
  65.     row = curwp->w_toprow;            /* Determine row.    */
  66.     clp = curwp->w_linep;
  67.     while (clp!=curbp->b_linep && clp!=curwp->w_dotp) {
  68.         ++row;
  69.         clp = lforw(clp);
  70.     }
  71.     ++row;                    /* Convert to origin 1.    */
  72.     col = 0;                /* Determine column.    */
  73.     for (i=0; i<curwp->w_doto; ++i) {
  74.         c = lgetc(curwp->w_dotp, i);
  75.         if (c == '\t')
  76.             col |= 0x07;
  77.         else if (ISCTRL(c) != FALSE)
  78.             ++col;
  79.         ++col;
  80.     }
  81.     ++col;                    /* Convert to origin 1.    */
  82.     ratio = 0;                /* Ratio before dot.    */
  83.     if (nchar != 0) {
  84.         ratio = (100L*cchar) / nchar;
  85.         if (ratio==0 && cchar!=0)    /* Allow 0% only at the    */
  86.             ratio = 1;        /* start of the file.    */
  87.     }
  88.     eprintf("[CH:0%o Line:%d Row:%d Col:%d %d%% of %d]",
  89.         cbyte, cline, row, col, ratio, nchar);
  90.     return (TRUE);
  91. }
  92.  
  93. /*
  94.  * Twiddle the two characters on either side of
  95.  * dot. If dot is at the end of the line twiddle the
  96.  * two characters before it. Return with an error if dot
  97.  * is at the beginning of line; it seems to be a bit
  98.  * pointless to make this work. This fixes up a very
  99.  * common typo with a single stroke. Normally bound
  100.  * to "C-T". This always works within a line, so
  101.  * "WFEDIT" is good enough.
  102.  */
  103. twiddle(f, n, k)
  104. {
  105.     register LINE    *dotp;
  106.     register int    doto;
  107.     register int    cl;
  108.     register int    cr;
  109.  
  110.     dotp = curwp->w_dotp;
  111.     doto = curwp->w_doto;
  112.     if (doto==llength(dotp) && --doto<0)
  113.         return (FALSE);
  114.     cr = lgetc(dotp, doto);
  115.     if (--doto < 0)
  116.         return (FALSE);
  117.     cl = lgetc(dotp, doto);
  118.     lputc(dotp, doto+0, cr);
  119.     lputc(dotp, doto+1, cl);
  120.     lchange(WFEDIT);
  121.     return (TRUE);
  122. }
  123.  
  124. /*
  125.  * Quote the next character, and
  126.  * insert it into the buffer. All the characters
  127.  * are taken literally, with the exception of the newline,
  128.  * which always has its line splitting meaning. The character
  129.  * is always read, even if it is inserted 0 times, for
  130.  * regularity.
  131.  */
  132. quote(f, n, k)
  133. {
  134.     register int    s;
  135.     register int    c;
  136.  
  137.     if (kbdmop != NULL)
  138.         c = *kbdmop++;
  139.     else {
  140.         c = ttgetc();
  141.         if (kbdmip != NULL) {
  142.             if (kbdmip > &kbdm[NKBDM-4]) {
  143.                 ctrlg(FALSE, 0, KRANDOM);
  144.                 return (ABORT);
  145.             }
  146.             *kbdmip++ = c;
  147.         }
  148.     }
  149.     if (n < 0)
  150.         return (FALSE);
  151.     if (n == 0)
  152.         return (TRUE);
  153.     if (c == '\n') {
  154.         do {
  155.             s = lnewline();
  156.         } while (s==TRUE && --n);
  157.         return (s);
  158.     }
  159.     return (linsert(n, c));
  160. }
  161.  
  162. /*
  163.  * Ordinary text characters are bound to this function,
  164.  * which inserts them into the buffer. Characters marked as control
  165.  * characters (using the CTRL flag) may be remapped to their ASCII
  166.  * equivalent. This makes TAB (C-I) work right, and also makes the
  167.  * world look reasonable if a control character is bound to this
  168.  * this routine by hand. Any META or CTLX flags on the character
  169.  * are discarded. This is the only routine that actually looks
  170.  * the the "k" argument.
  171.  */
  172. selfinsert(f, n, k)
  173. {
  174.     register int    c;
  175.  
  176.     if (n < 0)
  177.         return (FALSE);
  178.     if (n == 0)
  179.         return (TRUE);
  180.     c = k & KCHAR;
  181.     if ((k&KCTRL)!=0 && c>='@' && c<='_')    /* ASCII-ify.        */
  182.         c -= '@';
  183.     return (linsert(n, c));
  184. }
  185.  
  186. /*
  187.  * Open up some blank space. The basic plan
  188.  * is to insert a bunch of newlines, and then back
  189.  * up over them. Everything is done by the subcommand
  190.  * procerssors. They even handle the looping. Normally
  191.  * this is bound to "C-O".
  192.  */
  193. openline(f, n, k)
  194. {
  195.     register int    i;
  196.     register int    s;
  197.  
  198.     if (n < 0)
  199.         return (FALSE);
  200.     if (n == 0)
  201.         return (TRUE);
  202.     i = n;                    /* Insert newlines.    */
  203.     do {
  204.         s = lnewline();
  205.     } while (s==TRUE && --i);
  206.     if (s == TRUE)                /* Then back up overtop    */
  207.         s = backchar(f, n, KRANDOM);    /* of them all.        */
  208.     return (s);
  209. }
  210.  
  211. /*
  212.  * Insert a newline.
  213.  * If you are at the end of the line and the
  214.  * next line is a blank line, just move into the
  215.  * blank line. This makes "C-O" and "C-X C-O" work
  216.  * nicely, and reduces the ammount of screen
  217.  * update that has to be done. This would not be
  218.  * as critical if screen update were a lot
  219.  * more efficient.
  220.  */
  221. newline(f, n, k)
  222. {
  223.     register LINE    *lp;
  224.     register int    s;
  225.  
  226.     if (n < 0)
  227.         return (FALSE);
  228.     while (n--) {
  229.         lp = curwp->w_dotp;
  230.         if (llength(lp) == curwp->w_doto
  231.         && lp != curbp->b_linep
  232.         && llength(lforw(lp)) == 0) {
  233.             if ((s=forwchar(FALSE, 1, KRANDOM)) != TRUE)
  234.                 return (s);
  235.         } else if ((s=lnewline()) != TRUE)
  236.             return (s);
  237.     }
  238.     return (TRUE);
  239. }
  240.  
  241. /*
  242.  * Delete blank lines around dot.
  243.  * What this command does depends if dot is
  244.  * sitting on a blank line. If dot is sitting on a
  245.  * blank line, this command deletes all the blank lines
  246.  * above and below the current line. If it is sitting
  247.  * on a non blank line then it deletes all of the
  248.  * blank lines after the line. Normally this command
  249.  * is bound to "C-X C-O". Any argument is ignored.
  250.  */
  251. deblank(f, n, k)
  252. {
  253.     register LINE    *lp1;
  254.     register LINE    *lp2;
  255.     register int    nld;
  256.  
  257.     lp1 = curwp->w_dotp;
  258.     while (llength(lp1)==0 && (lp2=lback(lp1))!=curbp->b_linep)
  259.         lp1 = lp2;
  260.     lp2 = lp1;
  261.     nld = 0;
  262.     while ((lp2=lforw(lp2))!=curbp->b_linep && llength(lp2)==0)
  263.         ++nld;
  264.     if (nld == 0)
  265.         return (TRUE);
  266.     curwp->w_dotp = lforw(lp1);
  267.     curwp->w_doto = 0;
  268.     return (ldelete(nld, FALSE));
  269. }
  270.  
  271. /*
  272.  * Insert a newline, then enough
  273.  * tabs and spaces to duplicate the indentation
  274.  * of the previous line. Assumes tabs are every eight
  275.  * characters. Quite simple. Figure out the indentation
  276.  * of the current line. Insert a newline by calling
  277.  * the standard routine. Insert the indentation by
  278.  * inserting the right number of tabs and spaces.
  279.  * Return TRUE if all ok. Return FALSE if one
  280.  * of the subcomands failed. Normally bound
  281.  * to "C-J".
  282.  */
  283. indent(f, n, k)
  284. {
  285.     register int    nicol;
  286.     register int    c;
  287.     register int    i;
  288.  
  289.     if (n < 0)
  290.         return (FALSE);
  291.     while (n--) {
  292.         nicol = 0;
  293.         for (i=0; i<llength(curwp->w_dotp); ++i) {
  294.             c = lgetc(curwp->w_dotp, i);
  295.             if (c!=' ' && c!='\t')
  296.                 break;
  297.             if (c == '\t')
  298.                 nicol |= 0x07;
  299.             ++nicol;
  300.         }
  301.         if (lnewline() == FALSE
  302.         || ((i=nicol/8)!=0 && linsert(i, '\t')==FALSE)
  303.         || ((i=nicol%8)!=0 && linsert(i,  ' ')==FALSE))
  304.             return (FALSE);
  305.     }
  306.     return (TRUE);
  307. }
  308.  
  309. /*
  310.  * Delete forward. This is real
  311.  * easy, because the basic delete routine does
  312.  * all of the work. Watches for negative arguments,
  313.  * and does the right thing. If any argument is
  314.  * present, it kills rather than deletes, to prevent
  315.  * loss of text if typed with a big argument.
  316.  * Normally bound to "C-D".
  317.  */
  318. forwdel(f, n, k)
  319. {
  320.     if (n < 0)
  321.         return (backdel(f, -n, KRANDOM));
  322.     if (f != FALSE) {            /* Really a kill.    */
  323.         if ((lastflag&CFKILL) == 0)
  324.             kdelete();
  325.         thisflag |= CFKILL;
  326.     }
  327.     return (ldelete(n, f));
  328. }
  329.  
  330. /*
  331.  * Delete backwards. This is quite easy too,
  332.  * because it's all done with other functions. Just
  333.  * move the cursor back, and delete forwards.
  334.  * Like delete forward, this actually does a kill
  335.  * if presented with an argument.
  336.  */
  337. backdel(f, n, k)
  338. {
  339.     register int    s;
  340.  
  341.     if (n < 0)
  342.         return (forwdel(f, -n, KRANDOM));
  343.     if (f != FALSE) {            /* Really a kill.    */
  344.         if ((lastflag&CFKILL) == 0)
  345.             kdelete();
  346.         thisflag |= CFKILL;
  347.     }
  348.     if ((s=backchar(f, n, KRANDOM)) == TRUE)
  349.         s = ldelete(n, f);
  350.     return (s);
  351. }
  352.  
  353. /*
  354.  * Kill line. If called without an argument,
  355.  * it kills from dot to the end of the line, unless it
  356.  * is at the end of the line, when it kills the newline.
  357.  * If called with an argument of 0, it kills from the
  358.  * start of the line to dot. If called with a positive
  359.  * argument, it kills from dot forward over that number
  360.  * of newlines. If called with a negative argument it
  361.  * kills any text before dot on the current line,
  362.  * then it kills back abs(arg) lines.
  363.  */
  364. killline(f, n, k)
  365. {
  366.     register int    chunk;
  367.     register LINE    *nextp;
  368.  
  369.     if ((lastflag&CFKILL) == 0)        /* Clear kill buffer if    */
  370.         kdelete();            /* last wasn't a kill.    */
  371.     thisflag |= CFKILL;
  372.     if (f == FALSE) {
  373.         chunk = llength(curwp->w_dotp)-curwp->w_doto;
  374.         if (chunk == 0)
  375.             chunk = 1;
  376.     } else if (n > 0) {
  377.         chunk = llength(curwp->w_dotp)-curwp->w_doto+1;
  378.         nextp = lforw(curwp->w_dotp);
  379.         while (--n) {
  380.             if (nextp == curbp->b_linep)
  381.                 return (FALSE);
  382.             chunk += llength(nextp)+1;
  383.             nextp = lforw(nextp);
  384.         }
  385.     } else {                /* n <= 0        */
  386.         chunk = curwp->w_doto;
  387.         curwp->w_doto = 0;
  388.         while (n++) {
  389.             if (lback(curwp->w_dotp) == curbp->b_linep)
  390.                 break;
  391.             curwp->w_dotp = lback(curwp->w_dotp);
  392.             curwp->w_flag |= WFMOVE;
  393.             chunk += llength(curwp->w_dotp)+1;
  394.         }
  395.     }
  396.     return (ldelete(chunk, TRUE));
  397. }
  398.  
  399. /*
  400.  * Yank text back from the kill buffer. This
  401.  * is really easy. All of the work is done by the
  402.  * standard insert routines. All you do is run the loop,
  403.  * and check for errors. The blank
  404.  * lines are inserted with a call to "newline"
  405.  * instead of a call to "lnewline" so that the magic
  406.  * stuff that happens when you type a carriage
  407.  * return also happens when a carriage return is
  408.  * yanked back from the kill buffer.
  409.  * An attempt has been made to fix the cosmetic bug
  410.  * associated with a yank when dot is on the top line of
  411.  * the window (nothing moves, because all of the new
  412.  * text landed off screen).
  413.  */
  414. yank(f, n, k)
  415. {
  416.     register int    c;
  417.     register int    i;
  418.     register LINE    *lp;
  419.     register int    nline;
  420.  
  421.     if (n < 0)
  422.         return (FALSE);
  423.     nline = 0;                /* Newline counting.    */
  424.     while (n--) {
  425.         isetmark();            /* region around yank    */
  426.         i = 0;
  427.         while ((c=kremove(i)) >= 0) {
  428.             if (c == '\n') {
  429.                 if (newline(FALSE, 1, KRANDOM) == FALSE)
  430.                     return (FALSE);
  431.                 ++nline;
  432.             } else {
  433.                 if (linsert(1, c) == FALSE)
  434.                     return (FALSE);
  435.             }
  436.             ++i;
  437.         }
  438.     }
  439.     lp = curwp->w_linep;            /* Cosmetic adjustment    */
  440.     if (curwp->w_dotp == lp) {        /* if offscreen insert.    */
  441.         while (nline-- && lback(lp)!=curbp->b_linep)
  442.             lp = lback(lp);
  443.         curwp->w_linep = lp;        /* Adjust framing.    */
  444.         curwp->w_flag |= WFHARD;
  445.     }
  446.     return (TRUE);
  447. }
  448.